package thread;

import java.util.Random;

/**
 * Created by Administrator on 2017/10/19 0019.
 */
public class Prictice2
{
    public static void main(String[] args)
    {
        Car car1 = new Car("奥迪Q7");
        Car car2 = new Car("迈巴赫");
        Car car3 = new Car("劳斯莱斯幻影");
        Car car4 = new Car("路虎揽胜");
        Car car5 = new Car("宾利飞驰");

        ParkLocation p1 = new ParkLocation("一号库");
        ParkLocation p2 = new ParkLocation("二号库");
        ParkLocation p3 = new ParkLocation("三号库");

        Park p = new Park();
        p.position[0] = p1;
        p.position[1] = p2;
        p.position[2] = p3;
        SimulateThread s1 = new SimulateThread(car1, p);
        SimulateThread s2 = new SimulateThread(car2, p);
        SimulateThread s3 = new SimulateThread(car3, p);
        SimulateThread s4 = new SimulateThread(car4, p);
        SimulateThread s5 = new SimulateThread(car5, p);
        s1.start();
        s2.start();
        s3.start();
        s4.start();
        s5.start();

    }

    static class Park
    {
        public ParkLocation[] position = new ParkLocation[3];

        public void out()
        {
            for (int i = 0; i < position.length; i++)
            {
                position[i].out();
            }
        }

        public void in(Car car, int posi)
        {
            position[posi].in(car);
        }
    }

    static class ParkLocation
    {
        public String name;
        public boolean isFull;
        public Car car;

        public ParkLocation(String name)
        {
            this.name = name;
        }

        public synchronized void in(Car c)
        {
            if(isFull)
            {
                System.out.println(c.carNum + "试图停车在"+name+"，但车位已有车");
            }else
            {
                car = c;
                isFull = true;
                System.out.println(c.carNum + "在"+name+"停车入库");
            }
        }

        public synchronized void out()
        {
            if(isFull)
            {
                System.out.println(car.carNum + "在"+name+"出车");
                isFull = false;
                car = null;
            }else
            {
                System.out.println(name+"没有车辆需要出车");
            }
        }
    }

    static class Car
    {
        public String carNum;

        public Car(String carNum)
        {
            this.carNum = carNum;
        }
    }

    static class SimulateThread extends Thread
    {
        Random rd = new Random();

        Car car;
        Park park;
        public SimulateThread(Car car, Park park)
        {
            this.car = car;
            this.park = park;
        }

        @Override
        public void run()
        {
            while(true)
            {
                boolean outOrIn = rd.nextBoolean();
                int sleepTime = rd.nextInt(4000)+1000;
                int stopPosi = rd.nextInt(3);
                if(outOrIn)
                {
                    park.out();
                }else
                {
                    park.in(car, stopPosi);
                }
                try{
                    Thread.sleep(sleepTime);
                }catch (Exception e){}
            }
        }
    }
}
